<?php

/**
 * CodeRS - Atviras kodas Lietuvai
 * Copyright (C) 2007 CodeRS www.coders.lt info@coders.lt
 * MightMedia TVS
 * bendras.php - nurodome kokius veiksmus atlikti prieš puslapiui užsikraunant
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * 
 * Ši programa yra laisva. Jūs galite ją platinti ir/arba modifikuoti
 * remdamiesi Free Software Foundation paskelbtomis GNU Bendrosios
 * Viešosios licencijos sąlygomis: 2 licencijos versija, arba (savo
 * nuožiūra) bet kuria vėlesne versija.
 * 
 * Ši programa platinama su viltimi, kad ji bus naudinga, bet BE JOKIOS
 * GARANTIJOS; be jokios numanomos PERKAMUMO ar TINKAMUMO KONKRETIEMS
 * TIKSLAMS garantijos. Žiūrėkite GNU Bendrąją Viešąją licenciją norėdami
 * sužinoti smulkmenas.
 * 
 * Jūs turėjote kartu su šia programa gauti ir GNU Bendrosios Viešosios
 * licencijos kopija; jei ne - rašykite Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/

// Apsauga nuo GET: XSS ir MySQL injections
$QUERY_STRING = $_SERVER['QUERY_STRING'];
if (!empty($QUERY_STRING)) {
	if (preg_match("/SERVER|http|<|>|%3c|%3e|SELECT|UNION|UPDATE|INSERT/i", $QUERY_STRING) ||
	(eregi("<[^>]*script*\"?[^>]*>", $QUERY_STRING)) || (eregi("<[^>]*object*\"?[^>]*>", $QUERY_STRING)) ||
	(eregi("<[^>]*iframe*\"?[^>]*>", $QUERY_STRING)) || (eregi("<[^>]*applet*\"?[^>]*>", $QUERY_STRING)) ||
	(eregi("<[^>]*meta*\"?[^>]*>", $QUERY_STRING)) || (eregi("<[^>]*style*\"?[^>]*>", $QUERY_STRING)) ||
	(eregi("<[^>]*form*\"?[^>]*>", $QUERY_STRING)) || (eregi("\([^>]*\"?[^)]*\)", $QUERY_STRING)) ||
	(eregi("\"", $QUERY_STRING))) {
		$ip = (isset($_SERVER["HTTP_CLIENT_IP"]) && !empty($_SERVER["HTTP_CLIENT_IP"]) ? $_SERVER["HTTP_CLIENT_IP"] : 'N/A');
		$forwarded = (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && !empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : 'N/A');
		$remoteaddress = (isset($_SERVER["REMOTE_ADDR"]) && !empty($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'N/A');

		$message = "Failas: ".$_SERVER['PHP_SELF']."\n Užklausa: ".$QUERY_STRING."\n Tipas: ".$_SERVER['REQUEST_METHOD']."\n IP: (ip-forw-RA):- ".$ip." - ".$forwarded." - ".$remoteaddress."\n";
		ban(str_replace("\n",'',$message));
		if (!@mail("projektas@gmail.com", "BOMŽAS - ".date('Y-m-d'), $message, "From: host@".$_SERVER['SERVER_NAME'])) echo nl2br($message);

		die("<hr /><h1>Bomžams ne!</h1>");
	}
}

// IP baninimas ant serverio
function ban($message,$ip='',$status='deny') {
	if (!isset($ip) || empty($ip)) { $ip = $_SERVER['REMOTE_ADDR']; }
	if ($ip != '127.0.0.1') {
		$file = '.htaccess';
		if (file_exists($file) && is_writable($file)) {
			$fh = fopen($file,'a');
			fwrite($fh,"\n#".$message."\n".$status.' from '.$ip."\n");
			fclose($fh);
		}
	}
}

// Klaidų rodymas
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');

// Surandam config.php
$home_dir = "";
while (!file_exists($home_dir."variklis/config.php")) { $home_dir .= "../"; }
define("HOME_DIR", $home_dir);	//nustatom root direktorija "../"

//Tikrinam ar įdiegta TVS
if (!file_exists(HOME_DIR."variklis/config.php")) { header("Location: setup.php"); exit; } //Jei nėra failo, vykdom TVS diegimą

//
//DB Prisijungimas
//
require (HOME_DIR."variklis/config.php");
require (HOME_DIR."variklis/class/mysql.php");
$db = new sql($host, $user, $pass, $dbo);
unset($host,$user,$pass,$dbo);

$db->uzklausa("SET NAMES utf8");	//butina, lietuviskom raidem
$conf = $db->masyvas($db->uzklausa("SELECT * FROM `".LENTELES_PRIESAGA."nustatymai` LIMIT 1"));

//
//Nustom sistemos kintamuosius
//
define("OK",TRUE);
define("IP", $_SERVER['REMOTE_ADDR']);
define("DOMAIN", "");
define("PSL", strip_tags($_SERVER['QUERY_STRING']));
define("NARSYKLE", (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string)$_SERVER['HTTP_USER_AGENT']) : 'N/A');

define("T_USERS", LENTELES_PRIESAGA."users");
define("T_SESSIONS", LENTELES_PRIESAGA."sessions");
define("T_SESSIONS_KEYS", LENTELES_PRIESAGA."sessions_keys");
define("T_SAUKYKLA", LENTELES_PRIESAGA."saukykla");

define("QUOTES",get_magic_quotes_gpc());

//
// Sesijos
//
require(HOME_DIR."variklis/class/session.php");
$ses = new session;

$loginERROR = "";
if (isset($_GET['atsijungti'])) $ses->atsijungti();
if(isset($_POST['prisijungimas'])) $ses->prisijungti();

$ses->tikrinti();
// Jeigu kartais netycia nebutu nusistates PRISIJUNGES:
if (!defined('PRISIJUNGES')) {
	define("PRISIJUNGES", false);
}

$ses->session_isvalyti();

//Vartotojo duomenys
define("USER_ID", (PRISIJUNGES) ? $ses->user_data['user_id'] : 1);
define("USER_RIGHTS", (PRISIJUNGES) ? $ses->user_data['teises'] : '');
define("USER", (PRISIJUNGES) ? $ses->user_data['lygis'] : 0);
define("LYGIS", (PRISIJUNGES) ? $ses->user_data['lygis'] : 0);
define("MOD", (PRISIJUNGES && $ses->user_data['lygis'] >= 20) ? true : false);
define("ADMIN", (PRISIJUNGES && $ses->user_data['lygis'] >= 30) ? true : false);
define("S_ADMIN", (PRISIJUNGES && $ses->user_data['lygis'] == 33) ? true : false);

//Teisių tikrinimas
function CheckRights($right) {
	if (ADMIN && in_array($right, explode(".", USER_RIGHTS))) {
		return true;
	} else {
		return false;
	}
}

//Užkraunam pagrindines funkcijas
if (file_exists(HOME_DIR."variklis/funkcijos.php")) { require (HOME_DIR."variklis/funkcijos.php"); }
else { header("Location: setup.php"); exit; }	//Jei klaida, vykdom TVS diegimą

if (file_exists(HOME_DIR."templates/".$conf['template']."/sfunkcijos.php")) { require (HOME_DIR."templates/".$conf['template']."/sfunkcijos.php"); }
else { header("Location: setup.php"); exit; }	//Jei klaida, vykdom TVS diegimą

//
//
//
require HOME_DIR."variklis/class/tpl.php";
$tpl = new template("./templates/".$conf['template']."/tpl/");
/*
$tpl->setBlock('c', array(
'AUTHOR' => stripslashes($author),
'KOMENTARAS' => stripslashes($komentaras),
'DATE' => $data,
'REDAGUOTI' => $keisti,
'TRINTI' => $trinti,
'IP' => $ip)
);
*/
require HOME_DIR."variklis/class/cache.php";
$cache = new cache;

?>
